介绍
人脸检测是一个十分广泛应用场景。 这里介绍使用facenet 进行人脸检测,并返回人脸位置的应用
本文中所涉及代码全都只是demo,别使用在生产环境。
本文代码库:https://github.com/humboldt-xie/detect-face-http
希望对你有所帮助
facenet
facenet 是一个使用tensorflow 进行人脸识别的开源库,我们可以依赖于它,进行人脸识别相关开发,进而降低难度与成本
依赖库
tensorflow==1.2
scipy
scikit-learn
opencv-python
h5py
matplotlib
Pillow
requests
psutil
可使用pip install 安装
人脸检测python实现
保存成文件: detect.py 供稍后http 服务使用
import tensorflow as tf
from scipy import misc
import numpy as np
import detect_face
import os
from os.path import join as pjoin
class Facedetect:
minsize = 20 # minimum size of face
threshold = [0.6, 0.7, 0.7] # three steps's threshold
factor = 0.709 # scale factor
def __init__(self):
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6)
self.sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
self.pnet, self.rnet, self.onet = detect_face.create_mtcnn(self.sess, './align')
self.minsize = 20 # minimum size of face
self.threshold = [0.6, 0.7, 0.7] # three steps's threshold
self.factor = 0.709 # scale factor
def detectStream(self,stream):
img = misc.imread(stream, mode='RGB')
img_size = np.asarray(img.shape)[0:2]
bounding_boxes, _ = detect_face.detect_face(img, self.minsize, self.pnet, self.rnet, self.onet, self.threshold, self.factor)
return bounding_boxes
def detect(self,file):
img = misc.imread(os.path.expanduser(file), mode='RGB')
img_size = np.asarray(img.shape)[0:2]
bounding_boxes, _ = detect_face.detect_face(img, self.minsize, self.pnet, self.rnet, self.onet, self.threshold, self.factor)
return bounding_boxes
face=Facedetect()
print(face.detect("data/Anthony_Hopkins_0001.jpg"))
print(face.detectStream(open("data/Anthony_Hopkins_0002.jpg")))
构建http 服务
依赖 flask http库: pip install flask
import detect
import json
from flask import Flask
from flask import request
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
f = request.files['file']
f.save('/tmp/tmp.jpg')
bb=detect.face.detect('/tmp/tmp.jpg')
return json.dumps(bb.tolist())
return 'Hello, World!'
@app.route('/uploadStream', methods=['GET', 'POST'])
def upload_stream_file():
if request.method == 'POST':
f = request.files['file']
bb=detect.face.detectStream(f.stream)
return json.dumps(bb.tolist())
return 'Hello, World!'
测试
这里上传文件,并获取结果
curl -v -F file=@data/Anthony_Hopkins_0001.jpg http://127.0.0.1:5000/upload
curl -v -F file=@data/Anthony_Hopkins_0002.jpg http://127.0.0.1:5000/uploadStream
测试结果:
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> POST /upload HTTP/1.1
> Host: 127.0.0.1:5000
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Length: 13421
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------5db25d87d0af4561
>
< HTTP/1.1 100 Continue
127.0.0.1 - - [12/Dec/2017 20:13:33] "POST /upload HTTP/1.1" 200 -
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: text/html; charset=utf-8
< Content-Length: 99
< Server: Werkzeug/0.12.2 Python/2.7.13
< Date: Tue, 12 Dec 2017 12:13:33 GMT
<
* Closing connection 0
[[73.3935416340828, 62.191927909851074, 174.6082882732153, 197.14300167560577, 0.9999266862869263]]
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> POST /uploadStream HTTP/1.1
> Host: 127.0.0.1:5000
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Length: 13234
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------ca31a26260725b71
>
< HTTP/1.1 100 Continue
127.0.0.1 - - [12/Dec/2017 20:13:34] "POST /uploadStream HTTP/1.1" 200 -
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: text/html; charset=utf-8
< Content-Length: 98
< Server: Werkzeug/0.12.2 Python/2.7.13
< Date: Tue, 12 Dec 2017 12:13:34 GMT
<
* Closing connection 0
[[72.0666309595108, 62.10347318649292, 170.0654730796814, 188.18555089831352, 0.9999877214431763]]%
最终,在返回结果的body里面,返回了json串,里面标明识别出的人脸。
比较两人脸相似度
待续… …